package com.pdg.android.splorr.states;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.pdg.android.common.GenericAlertHandler;
import com.pdg.android.common.ParentedBase;
import com.pdg.android.common.State;
import com.pdg.android.splorr.R;
import com.pdg.android.splorr.SplorrActivity;
import com.pdg.android.splorr.data.Door;
import com.pdg.android.splorr.data.Game;
import com.pdg.android.splorr.data.enums.GameState;

public class RoomSummaryState extends
		State<Game, SplorrActivity, GameState> {
	
	private class WallMarkDialogHandler extends ParentedBase<RoomSummaryState> implements DialogInterface.OnClickListener{
		private EditText editText;
		public WallMarkDialogHandler(RoomSummaryState theParent,EditText theEditText) {
			super(theParent);
			setEditText(theEditText);
		}
		private void setEditText(EditText theEditText) {
			editText = theEditText;
		}
		public EditText getEditText(){
			return editText;
		}
		public void onClick(DialogInterface dialog, int which) {
			switch(which){
			case DialogInterface.BUTTON_POSITIVE:
				getParent().setWallMark(getEditText().getText().toString());
				break;
			}
		}
	}
	
	@SuppressLint("UseSparseArrays")
	private Map<Integer,UICommand> buttonCommands = new HashMap<Integer,UICommand>();
	
	public RoomSummaryState(Game theData, SplorrActivity theMachine) {
		super(theData, theMachine, GameState.ROOM_SUMMARY);
	}

	public void setWallMark(String theWallMark) {
		getData().setWallMark(theWallMark);
		refreshDisplay();
	}

	public void searchForTrap() {
		getData().getUtilities().alertDialog(R.string.dialog_title_fail, R.string.dialog_message_cannot_do_that, R.string.dialog_button_ok, new GenericAlertHandler());
	}

	public void searchForDoor() {
		getData().getUtilities().alertDialog(R.string.dialog_title_fail, R.string.dialog_message_cannot_do_that, R.string.dialog_button_ok, new GenericAlertHandler());
	}

	public void unlockDoor() {
		getData().unlockDoor();
		refreshDisplay();
	}

	public void closeDoor() {
		getData().closeDoor();
		refreshDisplay();
		
	}

	public void openDoor() {
		getData().openDoor();
		refreshDisplay();
	}

	private void turnAround() {
		getData().turnAround();
		refreshDisplay();
	}

	private void turnRight() {
		getData().turnRight();
		refreshDisplay();
	}

	private void turnLeft() {
		getData().turnLeft();
		refreshDisplay();
	}

	private void moveAhead() {
		getData().moveAhead();
		refreshDisplay();
	}

	@Override
	protected void onInitialize() {
	}

	@Override
	protected void onEnter() {
		getMachine().setContentView(R.layout.play);
		refreshDisplay();
	}

	private void refreshDisplay() {
		TextView tv = (TextView)getMachine().findViewById(R.id.text);
		tv.setText(getData().getCurrentRoomSummary());
		updateButtons();
	}

	private void updateButtons() {
		clearButtons();
		buttonCommands.put(R.id.button_r2_c1, UICommand.TURN_LEFT);
		buttonCommands.put(R.id.button_r2_c3, UICommand.TURN_RIGHT);
		buttonCommands.put(R.id.button_r3_c2, UICommand.TURN_AROUND);
		Door theDoor = getData().getPlayerData().getCurrentRoom().getDoor(getData().getPlayerData().getFacing());
		buttonCommands.put(R.id.button_r1_c2, theDoor.getDoorState().getUICommand());
		buttonCommands.put(R.id.button_r1_c3, theDoor.getDoorState().getSecondaryUICommand());
		
		buttonCommands.put(R.id.button_r1_c1, UICommand.SEARCH_FOR_TRAP);
		
		if(getData().getPlayerData().getCurrentRoom().getVisibleItemCount()>0){
			buttonCommands.put(R.id.button_r2_c2, UICommand.VIEW_ROOM_ITEMS);
		}else{
			buttonCommands.put(R.id.button_r2_c2, UICommand.SEARCH_FOR_ITEMS);
		}
		
		buttonCommands.put(R.id.button_r4_c1,UICommand.PLAYER_INVENTORY);
		buttonCommands.put(R.id.button_r4_c3,UICommand.MAIN_MENU);
		
		Iterator<Integer> iterator = buttonCommands.keySet().iterator();
		while(iterator.hasNext()){
			Integer theButtonId = iterator.next();
			UICommand theCommand = buttonCommands.get(theButtonId);
			Button theButton = (Button)getMachine().findViewById(theButtonId);
			theButton.setText(getMachine().getString(theCommand.getStringId()));
		}
		
	}

	private void clearButtons() {
		buttonCommands.clear();
		buttonCommands.put(R.id.button_r1_c1, UICommand.NONE);
		buttonCommands.put(R.id.button_r1_c2, UICommand.NONE);
		buttonCommands.put(R.id.button_r1_c3, UICommand.NONE);
		buttonCommands.put(R.id.button_r2_c1, UICommand.NONE);
		buttonCommands.put(R.id.button_r2_c2, UICommand.NONE);
		buttonCommands.put(R.id.button_r2_c3, UICommand.NONE);
		buttonCommands.put(R.id.button_r3_c1, UICommand.NONE);
		buttonCommands.put(R.id.button_r3_c2, UICommand.NONE);
		buttonCommands.put(R.id.button_r3_c3, UICommand.NONE);
		buttonCommands.put(R.id.button_r4_c1, UICommand.NONE);
		buttonCommands.put(R.id.button_r4_c2, UICommand.NONE);
		buttonCommands.put(R.id.button_r4_c3, UICommand.NONE);
	}

	@Override
	protected void onLeave() {
	}

	@Override
	protected void onCleanUp() {
	}

	@Override
	protected void onButtonPressed(int theButtonId) {
		executeUICommand(buttonCommands.get(theButtonId));
	}

	private void executeUICommand(UICommand uiCommand) {
		switch(uiCommand){
		case TURN_LEFT:
			turnLeft();
			break;
		case TURN_RIGHT:
			turnRight();
			break;
		case TURN_AROUND:
			turnAround();
			break;
		case MOVE:
			moveAhead();
			break;
		case OPEN_DOOR:
			openDoor();
			break;
		case CLOSE_DOOR:
			closeDoor();
			break;
		case VIEW_ROOM_ITEMS:
			getMachine().setCurrent(GameState.ROOM_ITEMS);
			break;
		case MARK_WALL:
			markWall();
			break;
		case EXAMINE_LOCK:
			examineLock();
			break;
		case UNLOCK_DOOR:
			unlockDoor();
			break;
		case PLAYER_INVENTORY:
			getMachine().setCurrent(GameState.PLAYER_INVENTORY);
			break;
		case NONE:
			//do nothing
			break;
		case SEARCH_FOR_ITEMS:
			searchForItems();
			break;
		case SEARCH_FOR_TRAP:
			searchForTrap();
			break;
		case SEARCH_WALL:
			searchWall();
			break;
		case MAIN_MENU:
			mainMenu();
			break;
		}
	}

	private void mainMenu() {
		getMachine().setCurrent(GameState.MAIN_MENU);
	}

	private void searchWall() {
		getData().searchWall();
		refreshDisplay();
	}

	private void searchForItems() {
		getData().searchForItems();
		refreshDisplay();
	}

	private void examineLock() {
		getData().examineLock();
		refreshDisplay();
	}

	private void markWall() {
		EditText input = new EditText(getMachine());
		WallMarkDialogHandler handler = new WallMarkDialogHandler(this,input);
		new AlertDialog.Builder(getMachine())
	    .setTitle(getMachine().getString(R.string.wall_mark_title))
	    .setMessage(getMachine().getString(R.string.wall_mark_message))
	    .setView(input)
	    .setPositiveButton(getMachine().getString(R.string.dialog_button_ok), handler)
	    .setNegativeButton(getMachine().getString(R.string.dialog_button_cancel), handler).show();	
	}

}
